package com.myl.api.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.IllegalSQLInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
//import org.springframework.web.filter.StringTrimmerFilter;

/**
 * MyBatisPlusConfig
 * author: myl <ixiaomu@qq.com> 2023/12/19
 * tips:
 */
@Configuration
public class MyBatisPlusConfig {

//    @Bean
//    public FilterRegistrationBean<StringTrimmerFilter> stringTrimmerFilterRegistration() {
//        FilterRegistrationBean<StringTrimmerFilter> registration = new FilterRegistrationBean<>();
//        registration.setFilter(new StringTrimmerFilter());
//        registration.setOrder(Ordered.HIGHEST_PRECEDENCE);
//        return registration;
//    }

    /**
     *
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();

        // 添加分页插件
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));

        // 针对 update 和 delete 语句 作用: 阻止恶意的全表更新删除
        interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());

        // 插件为安全控制一部分，非法SQL拦截（人为定义的非法SQL）提前发现解决插件。
        interceptor.addInnerInterceptor(new IllegalSQLInnerInterceptor());

        return interceptor;
    }
}
